<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\duty\controller;

use app\common\controller\Admin;
use app\plan\controller\api\Record as RecordApi;
use app\plan\controller\api\Recorddetail as RecorddetailApi;
use app\admin\controller\ajax\Library as Ajax;


/**
 * 应急执行
 * @package app\plan
 */
class Record  extends Admin

{

    protected $api;

    public function _initialize()
    {
        parent::_initialize();
        $this->ajax= new Ajax();
        $this->api = new RecordApi();
        $this->detail= new RecorddetailApi();

    }
    /**
     * 首页
     *
     */
    public function index() {
        if(IS_AJAX){

            $recordList = $this->api->recordList();
            return $recordList;
        }
        return $this->fetch();
    }

    /**
     * 应急执行搜索
     */
    public function search($start=null,$end=null,$keyword=null) {
        if(IS_AJAX){
            $recordList = $this->api->search(input('start'),input('end'),input('keyword'));
            return $recordList;
        }
    }
    /**
     * 添加应急执行
     *
     */
    public function add() {
        if(IS_POST){
            $data = input('post.');
            return $this->api->add($data);
        }
        $eventList = model('duty/events')->column('Title','id');

        $this->assign('eventList',$eventList);

        $planList = model('plan/plan')->column('Title','id');

        $this->assign('planList',$planList);

        $userList=model('common/user')->column('userName','id');
        $this->assign('userList',$userList);
        return $this->fetch();
    }

    /**
     * 修改应急执行
     *
     */
    public function edit($recordId= null) {
        if(IS_POST){
            $data = input('post.');
            return $this->api->add($data);
        }
        $eventList = model('duty/events')->column('Title','id');
        $this->assign('eventList',$eventList);
        $planList = model('plan/plan')->column('Title','id');
        $this->assign('planList',$planList);
        $userList=model('common/user')->column('userName','id');
        $this->assign('userList',$userList);
        $recordList = $this->api->recordList($recordId)->getData();
        $this->assign('info',$recordList['data'][0]);
        return $this->fetch();
    }

    /**
     * 删除应急执行
     */
    public function del($recordId = null) {
        if(IS_AJAX){
            return $this->api->del($recordId);
        }
    }

    /**
     * 执行明细列表
     */
    public function recorddetailAdd($recordId = null,$eventId=null) {

        if(empty($recordId)){
            return 0;
        }
        if(IS_POST){
            $data = input('post.');
            $data['rescueExecId']=$recordId;
            return $this->detail->add($data);
        }

        $type = $this->ajax->getScriptType()->getData();
        $this->assign('recordId',$recordId);
        $this->assign('eventId',$eventId);
        $eventList=model('duty/events')->column('Title','id');
        $this->assign('eventList',$eventList);
        $this->assign('type',$type);
        $chargeperson = model('basic/unit')->column('chargePerson','id');
        $this->assign('chargeperson',$chargeperson);
        $status= $this->ajax->getimplementStatusType()->getData();
        $this->assign('status',$status);
        return $this->fetch();
    }

    /**
     * 修改执行明细
     *
     */
    public function recorddetailedit($recordId = null,$eventId=null,$recorddetailId = null) {
        if(empty($recordId)){
            return 0;
        }

        if(IS_POST){
            $data = input('post.');
            $data['rescueExecId']=$recordId;
            return $this->detail->edit($data,$recorddetailId);
        }

        $type = $this->ajax->getScriptType()->getData();
        $this->assign('recordId',$recordId);

        $this->assign('eventId',$eventId);

        $eventList=model('duty/events')->column('Title','id');

        $this->assign('eventList',$eventList);
        $this->assign('type',$type);
        $status= $this->ajax->getimplementStatusType()->getData();
        $this->assign('status',$status);

        $chargeperson=model('basic/unit')->column('chargePerson','id');
        $this->assign('chargeperson',$chargeperson);

        $recorddetailList = $this->detail->recorddetailList($recordId,$recorddetailId)->getData();
        $this->assign('info',$recorddetailList['data'][0]);

        return $this->fetch();
    }



    /**
     * 删除执行明细
     */
    public function recorddetaildel($recorddetailId = null) {

        if(IS_AJAX){

            return $this->detail->del($recorddetailId);
        }
    }

    /**
     * 申请审核
     * @param string $id
     */
    public function audit($id = null){
        return $this->api->audit($id);
    }







}



